<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>async函数</title>
</head>
<body>
    
</body>

<script>
    // 执行async函数，返回的都是Promise对象
    async function test1() {
        return 1;
    }

    async function test2() {
        return Promise.resolve(2);
    }

    const res1 = test1();
    const res2 = test2();

    console.log(res1);
    console.log(res2);

    // Promise.then 成功的情况 对应 await
    /**
     * await后：
     *  1. 跟一个Promise对象
     *  2. 跟一个普通的值
     *  3. 跟一个异步函数
    */
    async function test3() {
        const p3 = Promise.resolve(3);
        p3.then(res => console.log('res', res));

        const data = await p3;
        console.log('data', data);
    }
    // p3.then = await p3;
    test3();


    async function test4() {
        const data4 = await 4; // await Promise.resovle(4);
        console.log('data4', data4);
    }
    test4();

    async function test5() {
        const data5 = await test1();
        console.log('data5', data5);
    }
    test5();

    // Promise.catch 异常的情况 对应 try...catch
    async function test6() {
        const p3 = Promise.reject(6);

        try {
            const data6 = await p6;
            console.log('data6', data6);
        } catch(e) {
            console.log('e', e);
        }
    }
    // p3.then = await p3;
    test6();



</script>

</html>